package com.zhiyou100.crm.dao.impl;

import com.zhiyou100.crm.dao.UserDao;
import com.zhiyou100.crm.util.DBUtil1;
import com.zhiyou100.crm.util.Notice;
import com.zhiyou100.crm.util.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    DBUtil1 dbUtil1=new DBUtil1();
    ResultSet rs=null;
    User user=null;
    Notice notice=null;
    List<User> users=null;
    @Override
    public List<User> queryUserByName(String u_name) {
        users=new ArrayList<>();
        String sql="select s.* from (select a.*,b.department_name,c.role_name from user as a,department as b,role as c where a.department_id=b.department_id and a.role_id=c.role_id) as s where username=? or mobile=? or address=?";
        Object[] obj={u_name,u_name,u_name};
        rs=dbUtil1.select(sql,obj);
        try {
            if (rs.next()) {
                int user_id = rs.getInt("user_id");
                String username=rs.getString("username");
                String password = rs.getString("password");
                boolean is_admin = rs.getBoolean("is_admin");
                boolean is_system = rs.getBoolean("is_system");
                String department_name = rs.getString("department_name");
                String role_name = rs.getString("role_name");
                String mobile = rs.getString("mobile");
                String email = rs.getString("email");
                String update_time = rs.getString("update_time");
                boolean is_male = rs.getBoolean("is_male");
                user=new User(user_id,username,password);
                users.add(user);
            }
        }catch (SQLException e){
            e.printStackTrace();
            return null;
        }
        return users;
    }

    @Override
    public List<User> queryAllUser() {
        List<User> users1=new ArrayList<>();
        String sql="select a.*,b.department_name,c.role_name from user as a,department as b,role as c where a.department_id=b.department_id and a.role_id=c.role_id";
        rs=dbUtil1.select(sql,null);
        try {
            while (rs.next()) {
                int user_id = rs.getInt("user_id");
                String username=rs.getString("username");
                String department_name = rs.getString("department_name");
                String role_name = rs.getString("role_name");
                String mobile = rs.getString("mobile");
                String email = rs.getString("email");
                Timestamp update_time = rs.getTimestamp("update_time");
                user=new User(user_id,username,mobile,email,update_time,department_name,role_name);
                users1.add(user);
            }
        }catch (SQLException e){
            e.printStackTrace();
            return null;
        }
        return users1;
    }

    @Override
    public boolean updateUser(int u_id, User user) {
        String sql="update user set password=?,tel=?,is_male=? where user_id=?";
        Object [] obj={user.getPassword(),user.getTel(),user.getIs_male(),u_id};
        int a=dbUtil1.update(sql,obj);
        if(a>0){
            return true;
        }else {
            return  false;
        }
    }

    @Override
    public int addUser(User user) {
        String sql="insert into user(username,password,is_admin,department_id,role_id,is_male,mobile,address,age,tel,id_num,email,qq,hobby,education,card_num,nation,marry,remark,create_time,update_time) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object[] obj={user.getUsername(),user.getPassword(),0,user.getDepartment_id(),user.getRole_id(),user.getIs_male(),user.getMobile(),user.getAddress(),user.getAge(),user.getTel(),user.getId_num(),user.getEmail(),user.getQq(),user.getHobby(),user.getEducation(),user.getCard_num(),user.getNation(),user.getMarry(),user.getRemark(),user.getCreate_time(),user.getCreate_time()};
        return dbUtil1.update(sql,obj);
    }

    @Override
    public boolean deleteUser(int u_id) {
        String sql="delete from user where user_id=?";
        Object[] obj={u_id};
        int count=dbUtil1.update(sql,obj);
        if(count>0){
            return true;
        }else{
            return false;
        }
    }
}